home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6082 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [Help] I can't find my error.
  5. Date: 22 Feb 1996 07:53 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <22FEB199607532063@erich.triumf.ca>
  9. References: <4ggvgr$1b2@aurora.engr.LaTech.edu>
  10. NNTP-Posting-Host: erich.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4ggvgr$1b2@aurora.engr.LaTech.edu>, pluu@engr.LaTech.edu writes...
  14. >Hi, all;
  15. >Could anyone please tell me why the following program won't work?
  16. >*********************************************************
  17. >/* This program will ask your name and age; and then
  18. >will print your name and age for next year back */
  19. >#include <stdio.h>
  20. >main()
  21. >{
  22. >    char name;    
  23.  
  24. This reserves space to store _exactly_ one char - hardly enough for most names!
  25. Change it to 
  26.     char name[20];  /* or more - long enough to hold the largest */
  27.                  /*    expected name   */
  28.  
  29. >    int  age, next_age; 
  30. >    printf("%s\n","Please enter your name and age: ");
  31. >    scanf("%s%d\n", &name, &age);
  32.  
  33. This should be
  34.     scanf("%19s%d\n", name, &age);
  35. to limit the number of chars to the number that will fit in name[]
  36.  
  37. scanf() often causes problems when used for direct user input - it may be
  38. better to use fgets() into a suitable sized buffer, then sscanf() to parse the
  39. input.
  40.  
  41.  
  42.  
  43. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  44. Internet: bennett@triumf.ca         | of one another only when one can be
  45. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  46. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  47. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  48.  
  49.